package com.zuorenke.business.router;

import cn.strong.leke.fs.model.FileView;
import cn.strong.leke.model.BaseCache;
import cn.strong.leke.model.base.Clazz;
import cn.strong.leke.model.question.QuestionDTO;
import cn.strong.leke.model.user.UserBase;
import com.fasterxml.jackson.core.type.TypeReference;
import com.zuorenke.help.CacheUtils;
import com.zuorenke.help.TicketUtils;
import com.zuorenke.hessian.client.IFileTransRemoteService;
import com.zuorenke.hessian.client.IKlassRemoteService;
import com.zuorenke.hessian.client.ILessonRemoteService;
import com.zuorenke.hessian.client.IQuestionRemoteService;
import com.zuorenke.model.ClientUploadModel;
import com.zuorenke.model.Result;
import com.zuorenke.model.po.Lesson;
import com.zuorenke.rabbit.RabbitMQSender;
import com.zuorenke.serialize.support.json.JsonUtils;
import com.zuorenke.vertx.VertxUtil;
import com.zuorenke.vertx.anno.RequestBody;
import com.zuorenke.vertx.anno.RequestMethod;
import com.zuorenke.vertx.anno.Route;
import com.zuorenke.vertx.anno.Router;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-27 17:35:32
 **/
@Component
@Router("api/w")
@Slf4j
public class ApiW {
	
	@Resource
	private RabbitMQSender loadCourseWareSender;
	
	@Resource
	private RabbitMQSender bindLessonSender;
	
	@Resource
	private ILessonRemoteService lessonRemoteService;
	@Resource
	private IKlassRemoteService klassRemoteService;
	@Resource
	private IFileTransRemoteService fileTransRemoteService;
	
	@Resource
	private IQuestionRemoteService questionRemoteService;
	
	@Route(value = "clientUploadLog", method = HttpMethod.GET)
	public Result clientUploadLog(String data, String ticket, int hello) {
		Result result = new Result();
//		ticket;
//		try{
//			String userId = TicketUtils.getUserId(ticket);
//		}catch(Exception e){
//			log.info("exception e {}", e.getMessage());
//
//		}
//		UserBase userbase = (UserBase) CacheUtils.hget(BaseCache.PREFIX_USER_BASE, userId);
//		if (userbase == null) {
//			result.setMsg(ExceptionCst.NO_LOGIN);
//			result.setCode(500);
//			return result;
//		} else {
//			System.out.println(111);
//		}
		
		WebClientOptions webClientOptions = new WebClientOptions()
				.setSsl(true).setConnectTimeout(10000).setFollowRedirects(true).setMaxRedirects(5);
		WebClient webClient = WebClient.create(VertxUtil.getVertxInstance(), webClientOptions);
		MultiMap map = MultiMap.caseInsensitiveMultiMap();
//		map.set("ticket", "VG1jOVBRPT07TkQ0M056UStQak0zTmpjPTs2Ng==");
		map.set("data", "{\"ids\":[\"3b47656eddcd2a8df7ce65be60a35206pptx37119c\"]}");
//		webClient.postAbs("https://api.leke.cn/api/n/invoke.htm?ticket=VG1jOVBRPT07TkQ0M056UStQak0zTmpjPTs2Ng==&_m=getFiles&_s=fs")
		webClient.post(443, "api.leke.cn", "/api/n/invoke.htm")
				.timeout(10000).ssl(true)
//				.addQueryParam("ticket", "VGxSUlBRPT07SkNRaEpDSWs7NTQ1NA==")
				.addQueryParam("_m", "getFiles")
				.addQueryParam("_s", "fs")
				.sendForm(map, ar -> {
					if (ar.failed()) {
						System.out.println(ar.result().bodyAsString());
					} else {
						System.out.println(ar.result().statusCode());
						log.info("ar.result {}", ar.result().bodyAsString());
						System.out.println(ar.result().bodyAsString());
					}
				});
//		List<ClientUploadModel> clientUploadModels = JsonUtils.readList(data, ClientUploadModel.class);
//		log.info("lesson {}",lessonRemoteService.getLessonByLessonId(6205L));
//		repositorySender.send(JsonUtils.toJSON(result));
//		fileErrorSender.send("111");
//		for (ClientUploadModel model : clientUploadModels) {
//			System.out.println(model);
//		}
		log.info("first api : {}", data);
		return result;
	}
	
	@Route(value = "hello2", method = HttpMethod.GET)
	public Result hello(@RequestBody String data, String ticket, int hello) {
		Result result = new Result();
//		try {
//			String userId = TicketUtils.getUserId(ticket);
//		} catch (Exception e) {
//			log.info("exception e {}", e.getMessage());
//		}
//		Clazz clazz = klassRemoteService.getDeptClazzByStudentId(1481705L);
//		System.out.println(clazz.getSchoolId());
//		System.out.println(clazz.getClassName());
//		FileView fv = fileTransRemoteService.getFileViewByFileId("09a04af48e703c9d9feecba3aa5a8045mp44a3bcee");
//		System.out.println(fv);
//		System.out.println(JsonUtils.toJson(fv));
		Integer[] questionIds = {4811972,8727432,6148873,8727048,8727167,7946385,8727294,11237659,8727496,8727108,7363902,11236856,7313767,8727486,7051407,11234411,7946327,10859509,8727095,5789870,7946323,8727136,5789890,5908201,7313765,11234415,8727296,11260147,8727349,8727175,8727321,10859534,8726872,8727300,8727288,8726612,10859513,8727126,8727130,11230684,8727477,8727222,8727289,8727483,8727354,8727299,8726859,8727484,8727328,8727325,8727502,8726602,8727515,8727522,8727133,8726660,8726872,8727171,8727162,8727139,4811954,8726597,8727554,8726715,8727133,8727537,8727486,11256779,8727408,8726855,7342662,11236863,11236847,8726973,5789874,11236831,8727318,8726750,8727166,11256782,5789893,8727426,8727485,10859525,6148786,8727504,8727141,5789870,8727289,8727408,11256788,8727495,8726855,7313805,8727326,11260177,11234412,10859513,8726575,8727175,7313755,8727328,8727351,8727496,10859488,11236910,4766104,8727053,7946330,8727492,11257007,8727293,11236837,8727096,6148869,8727106,8727322,4811972,8727164,7342676,8727296,7342677,8727317,11236829,5908203,8726855,8727500,8727387,11257007,11236840,8726750,11236942,8727426,8727173,11234411,11237658,8727354,11260177,8727295,4804485,7051410,11237659,8727420,8726979,8726683,8727320,8727300,11236877,8727323,7313758,7363879,8727315,7051370,4763418,8727125,4765652,5908203,7946384,4811972,11236910,8744451,8764920,8741525,8745294,11259573,8875780,8764603,8864689,8740500,8864672,8764703,8764617,8864550,8787981,8851371,8864638,8744601,8797092,8744626,8756606};
		for(Integer questionId : questionIds){
			QuestionDTO question = questionRemoteService.getQuestionDTO(0L+questionId);
			System.out.println(JsonUtils.toJson(question));
		}
		return result;
	}
	
}
